using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Description;
using System.Web.Mvc;
using MovieAPI.BLL;
using MovieAPI.Models;
using Newtonsoft.Json.Linq;

namespace MovieAPI.Controllers
{

    [EnableCors(origins: "*", headers: "*", methods: "*"), RequireHttpsAttribute]
    public class MovieInfoesController : ApiController
    {


        // GET: api/MovieInfoes
        public IHttpActionResult GetMovieInfo()
        {
            using (filmEntities db = new filmEntities())
            {
                var Movie = db.MovieInfo.ToList();
                return Ok(Movie);
            }
        }

        public IHttpActionResult GetMovieName() 
        {
            using (filmEntities db = new filmEntities()) 
            {
                var MovieName = db.MovieInfo.Select(m => m.MovieName).ToList();
                return Ok(MovieName);
            }
        }
        public IHttpActionResult GetMovieInfos(int MovieType)
        {
            if (MovieType == 2)
            {
                var comingMovie = MovieInfoBLL.GetComingMovie();
                return Ok(comingMovie);
            }
            else if (MovieType == 1)
            {
                var result = MovieInfoBLL.GetOnMovie();
                return Ok(result);
            }
            else
            {
                return Ok(MovieInfoBLL.GetMovieInfos());
            }
        }
        // GET: api/MovieInfoes/5

        public IHttpActionResult GetMovieInfo(int id)
        {
            using (filmEntities db = new filmEntities())
            {
                return Ok(db.MovieInfo.Where(M => M.MovieID == id).FirstOrDefault());
            }
        }
        // PUT: api/MovieInfoes/5
        [ResponseType(typeof(void))]
        [System.Web.Http.HttpPut]
        public IHttpActionResult PutMovieInfo(dynamic Movie)
        {
            using (filmEntities db = new filmEntities())
            {
                int ID = Movie.ID;
                MovieInfo MovieInfo = db.MovieInfo.Where(M => M.MovieID == ID).FirstOrDefault();
                MovieInfo.MovieAddress = Movie.Movie.MovieAddress;
                MovieInfo.MovieName = Movie.Movie.MovieName;
                MovieInfo.MovieDate = Movie.Movie.MovieDate;
                MovieInfo.MovieImg = Movie.Movie.MovieImg;
                MovieInfo.MovieTime = Movie.Movie.MovieTime;
                MovieInfo.MovieType = Movie.Movie.MovieType;
                MovieInfo.MovieReview = Movie.Movie.MovieReview;
                db.SaveChanges();
                return CreatedAtRoute("DefaultApi", new { id = MovieInfo.MovieID }, MovieInfo);
            }
        }
        // POST: api/MovieInfoes
        [ResponseType(typeof(MovieInfo))]
        public IHttpActionResult PostMovieInfo(dynamic Movie)
        {
            using (filmEntities db = new filmEntities())
            {
                MovieInfo MovieInfo = new MovieInfo();
                MovieInfo.MovieAddress = Movie.Movie.MovieAddress;
                MovieInfo.MovieName = Movie.Movie.MovieName;
                MovieInfo.MovieDate = Movie.Movie.MovieDate;
                MovieInfo.MovieImg = Movie.Movie.MovieImg;
                MovieInfo.MovieTime = Movie.Movie.MovieTime;
                MovieInfo.MovieType = Movie.Movie.MovieType;
                MovieInfo.MovieReview = Movie.Movie.MovieReview;
                db.MovieInfo.Add(MovieInfo);
                db.SaveChanges();

                return CreatedAtRoute("DefaultApi", new { id = MovieInfo.MovieID }, MovieInfo);
            }
        }

        // DELETE: api/MovieInfoes/5
        [ResponseType(typeof(MovieInfo))]
        public IHttpActionResult DeleteMovieInfo(int id)
        {
            using (filmEntities db = new filmEntities())
            {


                MovieInfo movieInfo = db.MovieInfo.Find(id);
                if (movieInfo == null)
                {
                    return NotFound();
                }

                db.MovieInfo.Remove(movieInfo);
                db.SaveChanges();

                return Ok(movieInfo);
            }
        }

        protected override void Dispose(bool disposing)
        {
            using (filmEntities db = new filmEntities())
            {


                if (disposing)
                {
                    db.Dispose();
                }
                base.Dispose(disposing);
            }
        }

        private bool MovieInfoExists(int id)
        {
            using (filmEntities db = new filmEntities())
            {


                return db.MovieInfo.Count(e => e.MovieID == id) > 0;
            }
        }
        public IHttpActionResult GetMovieType()
        {
            using (filmEntities db = new filmEntities())
            {


                return Ok(MovieInfoBLL.MovieType());
            }
        }
        public IHttpActionResult GetMovieCon()
        {
            using (filmEntities db = new filmEntities())
            {
                return Ok(MovieInfoBLL.GetMovieCon());
            }
        }
        public IHttpActionResult GetMovieYear()
        {
            using (filmEntities db = new filmEntities())
            {
                return Ok(MovieInfoBLL.GetMovieYear());
            }
        }

        [System.Web.Http.HttpPost]
        public IHttpActionResult GetmovieByType([FromBody] dynamic data)
        {
            using (filmEntities db = new filmEntities())
            {
                string MovieType = null;
                JObject obj = JObject.Parse(data.ToString());
                if (!string.IsNullOrEmpty(obj["data"]["MovieType"].Value<string>()))
                {
                    MovieType = obj["data"]["MovieType"].Value<string>();
                }
                int? MovieYear = null;
                if (!string.IsNullOrEmpty(obj["data"]["MovieYear"].Value<string>()))
                {
                    MovieYear = int.Parse(obj["data"]["MovieYear"].Value<string>());
                }
                string MovieCon = null;
                if (!string.IsNullOrEmpty(obj["data"]["MovieCon"].Value<string>()))
                {
                    MovieCon = obj["data"]["MovieCon"].Value<string>();
                }
                string MovieIndex = obj["data"]["MovieIndex"].Value<string>();

                return Ok(MovieInfoBLL.GetMovieByType(MovieType, MovieYear, MovieCon, MovieIndex));
            }
        }
        public IHttpActionResult GetHotMovie()
        {
            using (filmEntities db = new filmEntities())
            {
                var Movie = db.Orderinfo.GroupBy(m => m.Schedule.MovieID).Select(m => new { ID = m.Key, Total = m.Count() }).OrderByDescending(m => m.Total).ToList();
                List<MovieInfo> MovieInfo = new List<MovieInfo>();
                foreach (var item in Movie)
                {
                    MovieInfo.Add(db.MovieInfo.Where(m => m.MovieID == item.ID).FirstOrDefault());
                }
                return Ok(MovieInfo);
            }
        }
        [System.Web.Http.HttpGet]
        public IHttpActionResult GetMovieByName(string Name)
        {
            using (filmEntities db = new filmEntities())
            {
                var movie = db.MovieInfo.Where(M => M.MovieName.Contains(Name)).ToList();
                return Ok(movie);

            }
        }
        [System.Web.Http.HttpGet]
        public IHttpActionResult GetMovieByID(int ID)
        {
            using (filmEntities db = new filmEntities())
            {
                var Movie = db.MovieInfo.Where(m => m.MovieID == ID).FirstOrDefault();
                return Ok(Movie);
            }

        }
    }

}